import sys
import os
import random
from datetime import datetime
from sqlalchemy.orm import Session

sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

from app.db import get_db, init_db
from app.models.models import Student, Preference, Gender, CleanlinessLevel, SleepingHabit
from app.auth import get_password_hash

# 预定义的兴趣标签列表
INTEREST_TAGS = [
    "运动", "音乐", "游戏", "阅读", "电影", "旅行", "摄影", "美食",
    "编程", "绘画", "舞蹈", "篮球", "足球", "动漫", "宠物", "烹饪",
    "健身", "瑜伽", "游泳", "爬山", "钢琴", "吉他", "唱歌", "写作",
    "园艺", "手工", "收藏", "滑板", "网球", "羽毛球"
]

# 随机姓氏
SURNAMES = [
    "李", "王", "张", "刘", "陈", "杨", "黄", "赵", "吴", "周",
    "徐", "孙", "马", "朱", "胡", "郭", "何", "高", "林", "罗"
]

# 随机名字
NAME_CHARS = [
    "伟", "芳", "娜", "秀英", "敏", "静", "丽", "强", "磊", "军",
    "洋", "勇", "艳", "杰", "娟", "涛", "明", "超", "秀兰", "霞",
    "平", "刚", "桂英", "博", "志强", "建华", "建国", "建军", "晓燕", "玲"
]

def generate_random_student_id():
    """生成8位随机学号"""
    return ''.join([str(random.randint(0, 9)) for _ in range(8)])

def generate_random_name():
    """生成随机中文姓名"""
    surname = random.choice(SURNAMES)
    name = random.choice(NAME_CHARS)
    return surname + name

def generate_random_email(student_id):
    """生成随机邮箱"""
    return f"student_{student_id}@university.edu"

def generate_test_data():
    """生成测试数据"""
    db = next(get_db())
    try:
        print("开始生成测试数据...")
        
        # 生成50个男生和50个女生
        for gender in [Gender.MALE, Gender.FEMALE]:
            for _ in range(50):
                # 生成基本信息
                student_id = generate_random_student_id()
                name = generate_random_name()
                email = generate_random_email(student_id)
                
                # 创建学生
                student = Student(
                    student_id=student_id,
                    name=name,
                    email=email,
                    gender=gender,
                    hashed_password=get_password_hash("123456"),  # 默认密码
                    grade=random.randint(1, 4),  # 随机年级
                    major=random.choice(["计算机科学", "软件工程", "信息安全", "人工智能", "数据科学"]),
                    sleeping_habit=random.choice(list(SleepingHabit)),
                    cleanliness=random.choice(list(CleanlinessLevel)),
                    is_active=True
                )
                
                db.add(student)
                db.flush()  # 获取student.id
                
                # 创建偏好设置
                preference = Preference(
                    student_id=student.id,
                    interest_tags=random.sample(INTEREST_TAGS, 5)  # 随机选择5个兴趣标签
                )
                
                db.add(preference)
                print(f"已创建学生: {name} ({gender.value})")
        
        db.commit()
        print("测试数据生成完成！")
        
    except Exception as e:
        db.rollback()
        print(f"生成测试数据时出错: {str(e)}")
    finally:
        db.close()

if __name__ == "__main__":
    print("初始化数据库...")
    init_db()
    print("开始生成测试数据...")
    generate_test_data() 